package com.ruoyi.combat.service.useralignment.impl;

import java.io.File;
import java.nio.charset.Charset;
import java.util.*;

import cn.hutool.core.io.FileUtil;
import cn.hutool.json.JSONUtil;
import com.ruoyi.combat.domain.useralignment.*;
import com.ruoyi.combat.domain.userlocation.ConfuseUser;
import com.ruoyi.combat.mapper.useralignment.*;
import com.ruoyi.combat.mapper.userlocation.ConfuseUserMapper;
import com.ruoyi.combat.service.externalServiceInvoker.NetworkUserIdentityProtectionService;
import com.ruoyi.combat.util.weibo.WeiboApiUtil;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.ruoyi.combat.service.useralignment.ICombatAlignmentTaskService;

/**
 * 身份对齐任务Service业务层处理
 *
 * @author ruoyi
 * @date 2025-03-25
 */
@Service
public class CombatAlignmentTaskServiceImpl implements ICombatAlignmentTaskService {
    @Autowired
    private CombatAlignmentTaskMapper combatAlignmentTaskMapper;
    @Autowired
    private NetworkUserIdentityProtectionService networkUserIdentityProtectionService;
    @Autowired
    private CombatAlignmentWarnMapper combatAlignmentWarnMapper;
    @Autowired
    private CombatRelationWbMapper combatRelationWbMapper;
    @Autowired
    private CombatRelationZhMapper combatRelationZhMapper;
    @Autowired
    private CombatUserWbMapper combatUserWbMapper;
    @Autowired
    private ConfuseUserMapper confuseUserMapper;

    @Autowired
    private CombatHeavyUserMapper combatHeavyUserMapper;
    @Autowired
    private CombatUserZhMapper combatUserZhMapper;
    @Autowired
    private WeiboApiUtil weiboApiUtil;


    @Value("${keti5.funcation1Path}")
    private String funcation1Path;
    @Value("${keti5.callBackUrl1}")
    private String callBackUrl1;

    @Value("${keti5.funcation2Path}")
    private String funcation2Path;
    @Value("${keti5.callBackUrl2}")
    private String callBackUrl2;

    /**
     * 查询身份对齐任务
     *
     * @param id 身份对齐任务主键
     * @return 身份对齐任务
     */
    @Override
    public CombatAlignmentTaskVo selectCombatAlignmentTaskById(Long id) {
        CombatAlignmentTask task = combatAlignmentTaskMapper.selectCombatAlignmentTaskById(id);
        CombatAlignmentTaskVo bean1 = JSONUtil.toBean(JSONUtil.toJsonStr(task), CombatAlignmentTaskVo.class);
        String taskstate = task.getTaskstate();
        if ("0".equals(taskstate)) {
            return bean1;
        }
        String tasktype = task.getTasktype();
        String taskResult = task.getTaskResult();
        String fromUserId = task.getFromUserId();
        if ("抗社区".equals(tasktype)) {
            Map bean = JSONUtil.toBean(taskResult, Map.class);
            String addNodeStr = String.valueOf(bean.get("addNode"));
            List<String> addNode = JSONUtil.toList(addNodeStr, String.class);
            Object o = bean.get("init");
            if (o != null) {
                String initstr = String.valueOf(o);
                CombatUserWb bean2 = JSONUtil.toBean(initstr, CombatUserWb.class);
                Map initData = new HashMap();
                initData.put("data", bean2);
                bean1.setInitData(initData);

                String addRelationStr = String.valueOf(bean.get("addRelation"));
                List<List> addRelationList = JSONUtil.toList(addRelationStr, List.class);

                String initCommunityStr = String.valueOf(bean.get("initCommunity"));
                List<String> initCommunityList = JSONUtil.toList(initCommunityStr, String.class);
                String newCommunityStr = String.valueOf(bean.get("newCommunity"));
                List<String> newCommunityList = JSONUtil.toList(newCommunityStr, String.class);

                CombatUserWb newUser = JSONUtil.toBean(initstr, CombatUserWb.class);
                List<CombatUserWb> children = newUser.getChildren();
                for(List list:addRelationList){

                }


                newUser.setOrgType("1");
                for (CombatUserWb child : children) {
                    String userId = child.getUserId();
                    if(initCommunityStr.contains(userId)&&newCommunityStr.contains(userId)){
                        child.setOrgType("1");//前后都在同一社区
                    } else if (initCommunityStr.contains(userId)&&!newCommunityStr.contains(userId)) {
                        child.setOrgType("2");//前在后不在同一个社区
                    }else if (!initCommunityStr.contains(userId)&&newCommunityStr.contains(userId)) {
                        child.setOrgType("3");//后在前不在同一个社区
                    }else {
                        child.setOrgType("4"); //前后都不在同一个社区
                    }
                    for (CombatUserWb childChild : child.getChildren()) {
                        String userId1 = childChild.getUserId();
                        if(initCommunityStr.contains(userId1)&&newCommunityStr.contains(userId1)){
                            childChild.setOrgType("1");
                        } else if (initCommunityStr.contains(userId1)&&!newCommunityStr.contains(userId1)) {
                            childChild.setOrgType("2");
                        }else if (!initCommunityStr.contains(userId1)&&newCommunityStr.contains(userId1)) {
                            childChild.setOrgType("3");
                        }else {
                            childChild.setOrgType("4");
                        }
                    }

                }
                Map newData = new HashMap();
                newData.put("data", newUser);
                bean1.setNowData(newData);
            }
        } else {
            Map bean = JSONUtil.toBean(taskResult, Map.class);
            List<CombatRelationWb> initData1 = JSONUtil.toList(JSONUtil.toJsonStr(bean.get("initData")), CombatRelationWb.class);
            List<String> nowData1 = JSONUtil.toList(JSONUtil.toJsonStr(bean.get("nowData")), String.class);

            List<CombatRelationWb> nowData2 = new ArrayList<>();
            nowData2.addAll(initData1);
            for (String userid : nowData1) {
                CombatRelationWb combatRelationWb = new CombatRelationWb();
                combatRelationWb.setUserid(fromUserId);
                combatRelationWb.setTargetid(userid);
                List<CombatRelationWb> combatRelationWbs = combatRelationWbMapper.selectCombatRelationWbList(combatRelationWb);
                nowData2.addAll(combatRelationWbs);
            }
            String warnId = task.getWarnId();
            Map init = getInfoByid(Long.parseLong(warnId), initData1);
            Map now = getInfoByid(Long.parseLong(warnId), nowData2);
            bean1.setInitData(init);
            bean1.setNowData(now);

        }
        return bean1;
    }


    public Map getInfoByid(Long id, List<CombatRelationWb> combatRelationWbs) {
        Map re = new HashMap();
        CombatAlignmentWarn combatAlignmentWarn = combatAlignmentWarnMapper.selectCombatAlignmentWarnById(id);
        String userFromId = combatAlignmentWarn.getUserFromId();
        String userToId = combatAlignmentWarn.getUserToId();
        CombatHeavyUser combatHeavyUser = new CombatHeavyUser();
        combatHeavyUser.setWbUserId(userFromId);
        combatHeavyUser.setZhUserId(userToId);
        List<CombatHeavyUser> combatHeavyUsers = combatHeavyUserMapper.selectCombatHeavyUserList(combatHeavyUser);
        CombatHeavyUser combatHeavyUser1 = combatHeavyUsers.get(0);
        re.put("name", combatHeavyUser1.getName());
        re.put("unit", combatHeavyUser1.getUnit());

        List<Map> weiboUserList = new ArrayList<>();
        for (CombatRelationWb combatRelationWb : combatRelationWbs) {
            Map map = new HashMap();
            String userid = combatRelationWb.getUserid();
            String targetid = combatRelationWb.getTargetid();
            CombatUserWb combatUserWb = combatUserWbMapper.selectCombatUserWbByUserId(userid);
            CombatUserWb combatUserWb1 = combatUserWbMapper.selectCombatUserWbByUserId(targetid);
            map.put("source", combatUserWb);
            if (combatUserWb1 == null) {
                combatUserWb1 = new CombatUserWb();
                combatUserWb1.setUserId(targetid);
                combatUserWb1.setUserName(combatRelationWb.getTargetName());
                List<CombatUserWb> children = new ArrayList<>();
                CombatRelationWb combatRelationWb2 = new CombatRelationWb();
                combatRelationWb2.setUserid(targetid);
                List<CombatRelationWb> list = combatRelationWbMapper.selectCombatRelationWbList(combatRelationWb2);
                for (CombatRelationWb relationWb : list) {
                    String targetid1 = relationWb.getTargetid();
                    String targetName = relationWb.getTargetName();
                    String avatar = relationWb.getAvatar();
                    CombatUserWb combatUserWb2 = new CombatUserWb();
                    combatUserWb2.setUserId(targetid1);
                    combatUserWb2.setUserName(targetName);
                    combatUserWb2.setUserNickname(targetName);
                    combatUserWb2.setAvatar(avatar);
                    children.add(combatUserWb2);
                }
                combatUserWb1.setChildren(children);
            }
            map.put("target", combatUserWb1);
            weiboUserList.add(map);
        }
        re.put("weiboUserList", weiboUserList);
        CombatRelationZh zh = new CombatRelationZh();
        zh.setUserid(userToId);
        List<CombatRelationZh> combatRelationZhs = combatRelationZhMapper.selectCombatRelationZhList(zh);
        List<Map> zhihuUserList = new ArrayList<>();
        for (CombatRelationZh combatRelationWb : combatRelationZhs) {
            Map map = new HashMap();
            String userid = combatRelationWb.getUserid();
            String targetid = combatRelationWb.getTargetid();
            CombatUserZh combatUserWb = combatUserZhMapper.selectCombatUserZhByUserId(userid);
            CombatUserZh combatUserWb1 = combatUserZhMapper.selectCombatUserZhByUserId(targetid);
            if (combatUserWb == null) {
                combatUserWb = new CombatUserZh();
                combatUserWb.setUserId(userid);
                combatUserWb.setUserName(combatRelationWb.getUsername());
            }
            map.put("source", combatUserWb);
            if (combatUserWb1 == null) {
                combatUserWb1 = new CombatUserZh();
                combatUserWb1.setUserId(targetid);
                combatUserWb1.setUserName(combatRelationWb.getTargetName());
            }
            map.put("target", combatUserWb1);
            zhihuUserList.add(map);
        }
        re.put("zhihuUserList", zhihuUserList);
        return re;
    }

    /**
     * 查询身份对齐任务列表
     *
     * @param combatAlignmentTask 身份对齐任务
     * @return 身份对齐任务
     */
    @Override
    public List<CombatAlignmentTask> selectCombatAlignmentTaskList(CombatAlignmentTask combatAlignmentTask) {
        return combatAlignmentTaskMapper.selectCombatAlignmentTaskList(combatAlignmentTask);
    }

    /**
     * 新增身份对齐任务
     *
     * @param combatAlignmentTask 身份对齐任务
     * @return 结果
     */
    @Override
    public int insertCombatAlignmentTask(CombatAlignmentTask combatAlignmentTask) {
        return combatAlignmentTaskMapper.insertCombatAlignmentTask(combatAlignmentTask);
    }

    /**
     * 修改身份对齐任务
     *
     * @param combatAlignmentTask 身份对齐任务
     * @return 结果
     */
    @Override
    public int updateCombatAlignmentTask(CombatAlignmentTask combatAlignmentTask) {
        return combatAlignmentTaskMapper.updateCombatAlignmentTask(combatAlignmentTask);
    }

    /**
     * 批量删除身份对齐任务
     *
     * @param ids 需要删除的身份对齐任务主键
     * @return 结果
     */
    @Override
    public int deleteCombatAlignmentTaskByIds(Long[] ids) {
        return combatAlignmentTaskMapper.deleteCombatAlignmentTaskByIds(ids);
    }

    /**
     * 删除身份对齐任务信息
     *
     * @param id 身份对齐任务主键
     * @return 结果
     */
    @Override
    public int deleteCombatAlignmentTaskById(Long id) {
        return combatAlignmentTaskMapper.deleteCombatAlignmentTaskById(id);
    }

    @Override
    public void AntiRecognition(String warnId, String methodId) {
        CombatAlignmentWarn combatAlignmentWarn = combatAlignmentWarnMapper.selectCombatAlignmentWarnById(Long.parseLong(warnId));
        String userFromId = combatAlignmentWarn.getUserFromId();
        String userToId = combatAlignmentWarn.getUserToId();
        String similarContent = combatAlignmentWarn.getSimilarContent();
        CombatAlignmentTask combatAlignmentTask = new CombatAlignmentTask();
        combatAlignmentTask.setFromUserId(userFromId);
        String userFromName = combatAlignmentWarn.getUserFromName();
        combatAlignmentTask.setFromUserName(userFromName);
        combatAlignmentTask.setToUserId(userToId);
        combatAlignmentTask.setToUserName(combatAlignmentWarn.getUserToName());
        String taskname = combatAlignmentWarn.getUserToGroup();
        combatAlignmentTask.setCreatime(new Date());
        combatAlignmentTask.setStartime(new Date());
        combatAlignmentTask.setWarnId(warnId);
        combatAlignmentTask.setTaskstate("1");
        combatAlignmentTask.setCreateby(SecurityUtils.getUsername());
        combatAlignmentTask.setTasktype("抗身份识别");
        combatAlignmentTask.setTaskname(taskname);
        combatAlignmentTask.setMethodId(Long.parseLong(methodId));

        if ("2".equals(methodId)) {
            taskname = userFromName + "虚拟用户布设策略" + taskname;
            combatAlignmentTask.setTaskname(taskname);
            combatAlignmentTaskMapper.insertCombatAlignmentTask(combatAlignmentTask);
            Map<String, String> pathMap = antiCorrelationMethod(userFromId, userToId, similarContent);
            String wbpath = pathMap.get("wbpath");
            String zhpath = pathMap.get("zhpath");
            String similarNodePath = pathMap.get("similarNodePath");
            combatAlignmentTask = networkUserIdentityProtectionService.antiAlignment("1", userFromId, 5, callBackUrl1, null, null, wbpath, zhpath, similarNodePath, combatAlignmentTask, warnId);
        } else if ("3".equals(methodId)) {
            taskname = userFromName + "虚拟用户混淆策略" + taskname;
            combatAlignmentTask.setTaskname(taskname);
            combatAlignmentTaskMapper.insertCombatAlignmentTask(combatAlignmentTask);
            Map<String, String> pathMap = antiCorrelationMethod(userFromId, userToId, similarContent);
            String wbpath = pathMap.get("wbpath");
            String zhpath = pathMap.get("zhpath");
            String similarNodePath = pathMap.get("similarNodePath");
//            combatAlignmentTask =networkUserIdentityProtectionService.antiAlignment("2", userFromId, null, callBackUrl1, 5, "0.6", wbpath, zhpath, similarNodePath, combatAlignmentTask,warnId);
            combatAlignmentTask = networkUserIdentityProtectionService.antiAlignment("1", userFromId, 5, callBackUrl1, null, null, wbpath, zhpath, similarNodePath, combatAlignmentTask, warnId);
        } else if ("1".equals(methodId)) {
            taskname = userFromName + "虚拟社交关系抗" + taskname;
            combatAlignmentTask.setTaskname(taskname);
            combatAlignmentTaskMapper.insertCombatAlignmentTask(combatAlignmentTask);
            List<Map> link = new ArrayList<>();
            CombatRelationWb combatRelationWb = new CombatRelationWb();
            combatRelationWb.setUserid(userFromId);
            List<CombatRelationWb> combatRelationWbs = combatRelationWbMapper.selectCombatRelationWbList(combatRelationWb);
            List<CombatRelationWb> list = new ArrayList<>();
            list.addAll(combatRelationWbs);
            CombatUserWb userWb = combatUserWbMapper.selectCombatUserWbByUserId(userFromId);
            List<CombatUserWb> userWbChild = new ArrayList<>();
            for (CombatRelationWb relationWb : combatRelationWbs) {
                String targetid = relationWb.getTargetid();
                CombatUserWb combatUserWb1 = new CombatUserWb();
                combatUserWb1.setUserId(relationWb.getTargetid());
                combatUserWb1.setUserNickname(relationWb.getTargetName());
                combatUserWb1.setUserName(relationWb.getTargetName());
                combatUserWb1.setAvatar(relationWb.getAvatar());
                combatRelationWb.setUserid(targetid);
                List<CombatRelationWb> combatRelationWbs1 = combatRelationWbMapper.selectCombatRelationWbList(combatRelationWb);
                list.addAll(combatRelationWbs1);
                List<CombatUserWb> combatUserWbs = new ArrayList<>();
                for (CombatRelationWb relation : combatRelationWbs1) {
                    CombatUserWb combatUserWb = new CombatUserWb();
                    combatUserWb.setUserId(relation.getTargetid());
                    combatUserWb.setUserNickname(relation.getTargetName());
                    combatUserWb.setUserName(relation.getTargetName());
                    combatUserWb.setAvatar(relation.getAvatar());
                    combatUserWbs.add(combatUserWb);
                }
                combatUserWb1.setChildren(combatUserWbs);
                userWbChild.add(combatUserWb1);
            }
            userWb.setChildren(userWbChild);

            Set<String> set = new HashSet<>();
            for (CombatRelationWb wb : combatRelationWbs) {
                String userid = wb.getUserid();
                String targetid = wb.getTargetid();
                set.add(userid);
                set.add(targetid);
                Map<String, String> map = new HashMap<>();
                map.put("source", userid);
                map.put("target", targetid);
                link.add(map);
            }
            List<String> fakeNode = new ArrayList<>();
            List<ConfuseUser> confuseUsers = confuseUserMapper.selectConfuseUserList(new ConfuseUser());
            for (ConfuseUser confuseUser : confuseUsers) {
                String userid = confuseUser.getUserId();
                fakeNode.add(userid);
            }

            Map data = new HashMap();
            data.put("node", set);
            data.put("link", link);
            data.put("fakeNode", fakeNode);

            String jsonStr = JSONUtil.toJsonStr(data);
            String path = funcation2Path + "data" + File.separator + "data.json";
            FileUtil.writeUtf8String(jsonStr, path);
            //抗社区检测
            Map<String, Object> bean = networkUserIdentityProtectionService.antiOverlap(path, "gmc", userFromId, callBackUrl2, combatAlignmentTask);
            combatAlignmentTask.setEndtime(new Date());
            combatAlignmentTask.setTasktype("抗社区");
            combatAlignmentTask.setTaskstate("2");

            Map stringObjectMap = JSONUtil.toBean(JSONUtil.toJsonStr(bean.get("result")), Map.class);


            List<List<String>> addRelation = (List<List<String>>) stringObjectMap.get("新增的边为");

            //添加微博关注关系
            for (List<String> strings : addRelation) {
                String source = strings.get(0);
                String target = strings.get(1);
                CombatUserWb combatUserWb = combatUserWbMapper.selectCombatUserWbByUserId(source);
                if (combatUserWb != null) {
                    String cookie = combatUserWb.getToken();
                    weiboApiUtil.tofollow(source, Arrays.asList(target));
                } else {
                    CombatUserWb combatUserWb1 = combatUserWbMapper.selectCombatUserWbByUserId(target);
                    if (combatUserWb1 != null) {
                        String cookie = combatUserWb1.getToken();
                        weiboApiUtil.tofollow(source, Arrays.asList(source));
                    }
                }
            }

            Map<String, Object> re = new HashMap<>();
            re.put("addNode", stringObjectMap.get("新增的节点为"));
            re.put("addRelation", stringObjectMap.get("新增的边为"));
            re.put("initCommunity", stringObjectMap.get("在初始社区划分中目标节点所属的社区为"));
            re.put("newCommunity", stringObjectMap.get("在对抗后的划分中目标节点所属的社区为"));
            re.put("initData", combatRelationWbs);
            re.put("init", userWb);
            combatAlignmentTask.setTaskResult(JSONUtil.toJsonStr(re));
            combatAlignmentTaskMapper.updateCombatAlignmentTask(combatAlignmentTask);
            combatAlignmentWarn.setStatus("2");
            combatAlignmentWarnMapper.updateCombatAlignmentWarn(combatAlignmentWarn);
        }
        List<Map<String, String>> listMap = new ArrayList<>();
        CombatUserWb combatUserWb = combatUserWbMapper.selectCombatUserWbByUserId(userFromId);
        Map<String, String> da = new HashMap<>();
        da.put("type", "follow");
        da.put("cookie", combatUserWb.getToken());
        da.put("user_id", userFromId);
        listMap.add(da);
        weiboApiUtil.getFriends(listMap);
    }


    public Map<String, String> antiCorrelationMethod(String userFromId, String userToId, String similarContent) {
        CombatRelationWb combatRelationWb = new CombatRelationWb();
        combatRelationWb.setUserid(userFromId);
        List<CombatRelationWb> wbLists = combatRelationWbMapper.selectCombatRelationWbList(combatRelationWb);
        List<CombatRelationWb> wbList = new ArrayList<>();
        wbList.addAll(wbLists);
        try {
            for (CombatRelationWb relationWb : wbLists) {
                String targetid = relationWb.getTargetid();
                combatRelationWb.setUserid(null);
                combatRelationWb.setTargetid(targetid);
                List<CombatRelationWb> combatRelationWbs1 = combatRelationWbMapper.selectCombatRelationWbList(combatRelationWb);
                wbList.addAll(combatRelationWbs1);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        CombatRelationZh combatRelationZh = new CombatRelationZh();
        combatRelationZh.setUserid(userToId);
        List<CombatRelationZh> zhList = combatRelationZhMapper.selectCombatRelationZhList(combatRelationZh);
        combatRelationZh.setUserid(null);
        combatRelationZh.setTargetid(userToId);
        List<CombatRelationZh> combatRelationZhs1 = combatRelationZhMapper.selectCombatRelationZhList(combatRelationZh);
        zhList.addAll(combatRelationZhs1);
        String time = String.valueOf(System.currentTimeMillis());
        List<CombatUserWb> combatUserWbs = combatUserWbMapper.selectCombatUserWbListBySimilarNodeNoNull(new CombatUserWb());
        return antiCorrelationMethod(wbList, zhList, combatUserWbs, time, similarContent);
    }

    public Map<String, String> antiCorrelationMethod(List<CombatRelationWb> wbList, List<CombatRelationZh> zhList, List<CombatUserWb> combatUserWbs, String time, String similarContent) {
        Map<String, String> result = new HashMap<>();
        List<String> wbDataList = new ArrayList<>();
        for (CombatRelationWb wb : wbList) {
            String userid1 = wb.getUserid();
            String targetid = wb.getTargetid();
            String data = userid1 + " " + targetid;
            wbDataList.add(data);
        }
        String path = funcation1Path + File.separator + "data" + File.separator + time + File.separator + "initial" + File.separator + "weibo.cites";

        FileUtil.writeLines(wbDataList, path, Charset.defaultCharset());
        List<String> zhDataList = new ArrayList<>();
        for (CombatRelationZh wb : zhList) {
            String userid1 = wb.getUserid();
            String targetid = wb.getTargetid();
            String data = userid1 + " " + targetid;
            zhDataList.add(data);
        }
        String path1 = funcation1Path + File.separator + "data" + File.separator + time + File.separator + "initial" + File.separator + "zhihu.cites";
        FileUtil.writeLines(zhDataList, path1, Charset.defaultCharset());
        //相似节点文件
        Map bean = JSONUtil.toBean(similarContent, Map.class);
        List<String> similarNodeList = new ArrayList<>();
        bean.keySet().stream().forEach(key -> {
            List<String> list = (List<String>) bean.get(key);
            String data = "";
            for (String s : list) {
                if ("".equals(s)) {
                    data = s;
                } else {
                    data = data + " " + s;
                }
            }
            similarNodeList.add(key + " " + data);
        });
        String path2 = funcation1Path + File.separator + "data" + File.separator + time + File.separator + "initial" + File.separator + "similar_node.txt";
        FileUtil.writeLines(zhDataList, path2, Charset.defaultCharset());

        result.put("wbpath", path);
        result.put("zhpath", path1);
        result.put("similarNodePath", path2);
        return result;
    }

    public static void main(String[] args) {
        String s = FileUtil.readString("C:\\Users\\HYZ\\Desktop\\Untitled-1(1).json", Charset.defaultCharset());
        Map bean3 = JSONUtil.toBean(s, Map.class);
        String s1 = String.valueOf(bean3.get("rows"));

        List<CombatAlignmentTaskVo> list1 = JSONUtil.toList(s1, CombatAlignmentTaskVo.class);
//        CombatAlignmentTask task = combatAlignmentTaskMapper.selectCombatAlignmentTaskById(id);
        CombatAlignmentTaskVo bean1 = list1.get(0);
        String taskstate = bean1.getTaskstate();
        if ("0".equals(taskstate)) {
            return ;
        }
        String tasktype = bean1.getTasktype();
        String taskResult = bean1.getTaskResult();
        String fromUserId = bean1.getFromUserId();
        if ("抗社区".equals(tasktype)) {
            Map bean = JSONUtil.toBean(taskResult, Map.class);
            String addNodeStr = String.valueOf(bean.get("addNode"));
            List<String> addNode = JSONUtil.toList(addNodeStr, String.class);
            Object o = bean.get("init");
            if (o != null) {
                String initstr = String.valueOf(o);
                CombatUserWb bean2 = JSONUtil.toBean(initstr, CombatUserWb.class);
                Map initData = new HashMap();
                initData.put("data", bean2);
                bean1.setInitData(initData);

                String addRelationStr = String.valueOf(bean.get("addRelation"));
                List<List> addRelationList = JSONUtil.toList(addRelationStr, List.class);

                String initCommunityStr = String.valueOf(bean.get("initCommunity"));
                List<String> initCommunityList = JSONUtil.toList(initCommunityStr, String.class);
                String newCommunityStr = String.valueOf(bean.get("newCommunity"));
                List<String> newCommunityList = JSONUtil.toList(newCommunityStr, String.class);

                CombatUserWb newUser = JSONUtil.toBean(initstr, CombatUserWb.class);
                List<CombatUserWb> children = newUser.getChildren();
                for(List list:addRelationList){

                }


                newUser.setOrgType("1");
                for (CombatUserWb child : children) {
                    String userId = child.getUserId();
                    if(initCommunityStr.contains(userId)&&newCommunityStr.contains(userId)){
                        child.setOrgType("1");//前后都在同一社区
                    } else if (initCommunityStr.contains(userId)&&!newCommunityStr.contains(userId)) {
                        child.setOrgType("2");//前在后不在同一个社区
                    }else if (!initCommunityStr.contains(userId)&&newCommunityStr.contains(userId)) {
                        child.setOrgType("3");//后在前不在同一个社区
                    }else {
                        child.setOrgType("4"); //前后都不在同一个社区
                    }
                    List<CombatUserWb> children1 = child.getChildren();
                    for (CombatUserWb childChild : children1) {
                        String userId1 = childChild.getUserId();
                        if(initCommunityStr.contains(userId1)&&newCommunityStr.contains(userId1)){
                            childChild.setOrgType("1");
                        } else if (initCommunityStr.contains(userId1)&&!newCommunityStr.contains(userId1)) {
                            childChild.setOrgType("2");
                        }else if (!initCommunityStr.contains(userId1)&&newCommunityStr.contains(userId1)) {
                            childChild.setOrgType("3");
                        }else {
                            childChild.setOrgType("4");
                        }
                    }

                }
                Map newData = new HashMap();
                newData.put("data", newUser);
                bean1.setNowData(newData);
            }
        } else {
//            Map bean = JSONUtil.toBean(taskResult, Map.class);
//            List<CombatRelationWb> initData1 = JSONUtil.toList(JSONUtil.toJsonStr(bean.get("initData")), CombatRelationWb.class);
//            List<String> nowData1 = JSONUtil.toList(JSONUtil.toJsonStr(bean.get("nowData")), String.class);
//
//            List<CombatRelationWb> nowData2 = new ArrayList<>();
//            nowData2.addAll(initData1);
//            for (String userid : nowData1) {
//                CombatRelationWb combatRelationWb = new CombatRelationWb();
//                combatRelationWb.setUserid(fromUserId);
//                combatRelationWb.setTargetid(userid);
//                List<CombatRelationWb> combatRelationWbs = combatRelationWbMapper.selectCombatRelationWbList(combatRelationWb);
//                nowData2.addAll(combatRelationWbs);
//            }
//            String warnId = task.getWarnId();
//            Map init = getInfoByid(Long.parseLong(warnId), initData1);
//            Map now = getInfoByid(Long.parseLong(warnId), nowData2);
//            bean1.setInitData(init);
//            bean1.setNowData(now);

        }
        return ;
    }


}
